package io.gitee.hefren.hhmq.model;

import com.google.common.base.MoreObjects;

/**
 * @Date 2024/7/23
 * @Author lifei
 */
public class Result<T> {

    private T data;
    private int code;

    public Result() {
    }

    public Result(T data, int code) {
        this.data = data;
        this.code = code;
    }

    public static <M> Result<M> ok(M data) {
        return createResult(data, CodeEnum.SUCCESS.getCode());
    }

    public static <M> Result<M> createResult(M data, int code) {
        return new Result<>(data, code);
    }

    public boolean isSuccess() {
        return code == CodeEnum.SUCCESS.getCode();
    }

    public enum CodeEnum {
        SUCCESS(1), FAIL(0);
        private final int code;

        CodeEnum(int code) {
            this.code = code;
        }

        public int getCode() {
            return code;
        }
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    @Override
    public String toString() {
        return MoreObjects.toStringHelper(Result.class)
                .add("code", code)
                .add("data", data)
                .toString();
    }
}
